<template>
  <Layer :layer="layer" @confirm="submit">
    <el-form :model="ruleForm" :rules="rules" ref="form" label-width="120px" style="margin-right: 30px">
      <el-form-item label="用户ID" v-if="layer.row != null">
        {{ ruleForm.userId }}
      </el-form-item>
      <el-form-item label="用户状态" prop="status">
        <el-radio-group v-model="ruleForm.status" size="small">
          <el-radio-button label="false">停止</el-radio-button>
          <el-radio-button label="true">启动</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="name">
        <el-input v-model="ruleForm.nickname" placeholder="请输入昵称"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="mobile">
        <el-input v-model="ruleForm.mobile" placeholder="请输入电话"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="ruleForm.email" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="email">
        <el-select v-model="ruleForm.roleId" multiple filterable allow-create style="width:100%" default-first-option
                   :reserve-keyword="false" placeholder="请选择角色">
          <el-option v-for="item in roleOptions" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
    </el-form>
  </Layer>
</template>

<script lang="ts" setup>
import {ref, toRefs, nextTick} from "vue";
import Layer from "@/components/layer/index.vue";
import {add, update} from "@/api/system/user";
import {getData} from "@/api/system/role";
import {ElMessage} from "element-plus";

const props = defineProps({
  layer: {
    type: Object,
    default() {
      return {
        show: false,
        title: "",
        showButton: true,
      };
    },
  },
});
const {layer} = toRefs(props);
const emits = defineEmits(["getTableData"]);
const form = ref();

let ruleForm = ref({
  userId: "",
  username: "",
  nickname: "",
  password: "",
  mobile: "",
  email: "",
  roleId: [],

  status: true,
});

const rules = {
  username: [{required: true, message: "请输入用户名", trigger: "blur"}]
};

const roleOptions: any = ref([])
// 选中图标

loadMenu()

function loadMenu() {
  let select: any = [];
  if (layer.value.row && layer.value.row.roleId) {
    select = JSON.parse(layer.value.row.roleId)
  }
  getData({}).then(({data}) => {
    data.forEach((res: any) => {
      roleOptions.value.push({
        value: res.roleId,
        label: res.name,
      })
    })
  })
  nextTick(() => {
    ruleForm.value.roleId = select
  })
}

init()

function init() { // 用于判断新增还是编辑功能
  if (layer.value.row) {
    ruleForm.value = JSON.parse(JSON.stringify(layer.value.row)) // 数量量少的直接使用这个转
  } else {
  }
}

const submit = () => {
  form.value.validate((valid: boolean) => {
    if (valid) {
      let params = ruleForm.value;
      if (layer.value.row) {
        updateForm(params);
      } else {
        addForm(params);
      }
    } else {
      return false;
    }
  });
};
// 新增提交事件
const addForm = (params: object) => {
  add(params).then((res) => {
    ElMessage({
      type: "success",
      message: "新增成功",
    });
    layer.value.show = false;
    emits("getTableData", true);
  });
};
// 编辑提交事件
const updateForm = (params: object) => {
  update(params).then((res) => {
    ElMessage({
      type: "success",
      message: "编辑成功",
    });
    emits("getTableData", false);
  });
}
{
}
</script>

<style lang="scss" scoped>
</style>